App.controller('codeController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {


    $scope.grid = {
        column_defs: [
            {
                target: 4,
                buttons: [

                    {
                        name: '编辑',
                        class: 'btn-edit',
                        icon: 'fa-edit',
                        event: 'update',
                        enable: function (row) {
                            return row.canDelete == "1";
                        }
                    },
                    {
                        name: '删除',
                        class: 'btn-delete',
                        icon: 'fa-trash-o',
                        event: 'delete',
                        enable: function (row) {
                            return row.canDelete == "1";
                        }
                    }
                ]
            }
        ],
        options: {lazy: true},

        add: function () {
            if (!$scope.codeType) {
                Page.notice('请先选择字典类型')
                return;
            }
            $scope.addFlag = true;
            $scope.grid.edit = true;
            $scope.showCodeType = false;
            $scope.model = {
                codeType: $scope.codeType.codeValue,
                parentCodeId: $scope.codeType.codeId
            }
        },
        save: function () {
            Page.ajaxPost($scope.addFlag ? 'code/add.do' : 'code/update.do', $scope.model, function () {
                if ($scope.model.codeType == 'codetype') {
                    $scope.init()
                }
                $scope.grid.cancel();
                if ($scope.codeType) {
                    var params = {codeId: $scope.codeType.codeId};
                    $scope.grid.query(params);
                }
            });

        },
        update: function (row) {
            $scope.addFlag = false;
            $scope.grid.edit = true;
            $scope.showCodeType = false;
            $scope.model = row;
        },
        delete: function (row) {
            Page.confirm("确定要删除吗?", function () {
                Page.ajaxPost('code/delete.do', row, function (resp) {
                    Page.notice('删除成功');
                    if ($scope.codeType) {
                        var params = {codeId: $scope.codeType.codeId};
                        $scope.grid.query(params);
                    }
                    $scope.grid.cancel();
                })
            })
        }
    }

    $scope.init = function () {
        Page.ajaxPost('code/typeTree.do', null, function (resp) {
            $scope.codeTypes = resp.data;
            $scope.typeTree = $.fn.zTree.init($("#codeTree1"), {
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "codeId",
                        pIdKey: "parentCodeId"
                    },
                    key: {
                        name: "codeName"
                    }
                },
                callback: {
                    onClick: function (event, treeId, treeNode) {
                        $scope.codeType = treeNode;
                        $scope.grid.query({codeId: treeNode.codeId});
                    }
                }
            }, $scope.codeTypes);
        });
    }

    $scope.init();

    $scope.addCodeType = function () {
        $scope.grid.edit = true;
        $scope.addFlag = true;
        $scope.showCodeType = true;
        $scope.model = {
            codeType: 'codetype'
        }
    }

    $scope.updateCodeType = function () {
        $scope.grid.edit = true;
        $scope.addFlag = false;
        $scope.showCodeType = true;
        $scope.model = $scope.codeType;
    }

    $scope.deleteCodeType = function () {
        Page.confirm("确定要删除吗?", function () {
            Page.ajaxPost('code/delete.do', $scope.codeType, function (resp) {
                Page.notice('删除成功');
                $scope.init();
                $scope.grid.cancel();
            })
        })
    }
}]);
